# encoding: utf-8
class CulturalHeritage::CulturalHeritagePreRegister < ActiveRecord::Base
  has_attached_file:avatar
  belongs_to :obj_pre_register, :class_name => 'CulturalHeritage::PreRegisterRequest',  :foreign_key => 'pre_registers_request'
  belongs_to :obj_culturalInstitution, :class_name => 'CulturalHeritage::CulturalInstitution',  :foreign_key => 'culturalInstitution'
  belongs_to :obj_conservational_state, :class_name => 'CulturalHeritage::ConservationalState',  :foreign_key => 'conservationalState'
  belongs_to :obj_integrity_state, :class_name => 'CulturalHeritage::IntegrityState',  :foreign_key => 'integrityState'
  belongs_to :obj_subGroup, :class_name => 'CulturalHeritage::SubGroup',  :foreign_key => 'subGroup'
  belongs_to :obj_group, :class_name => 'CulturalHeritage::Group',  :foreign_key => 'group'
  belongs_to :obj_tipology, :class_name => 'CulturalHeritage::Tipology',  :foreign_key => 'tipology'
  belongs_to :obj_typeCulturalInstitution, :class_name => 'CulturalHeritage::TypeCulturalInstitution',  :foreign_key => 'typeCulturalInstitution'
  belongs_to :obj_period, :class_name => 'CulturalHeritage::Period',  :foreign_key => 'period'
  belongs_to :obj_culture, :class_name => 'CulturalHeritage::Culture',  :foreign_key => 'culture'
  belongs_to :obj_owner, :class_name => 'Security::Person',  :foreign_key => 'owner'
  belongs_to :obj_author, :class_name => 'Security::Person',  :foreign_key => 'author'
  

  # validates :preRegisterRegistrationCode, :presence => {:message => "El código es requerido"}
#validates :preRegisterRegistrationCode, :length => { :is => 6, :message => "El código debe tener 8 caracteres de numeros" }
#validates :name, :presence => {:message => "El nombre es requerido"}
# validates :description, :presence => {:message => "La descripción es requerida"}
#  validates :foundlnld, :presence => {:message => "foundlnld es requerido"}
#  validates :height, :presence => {:message => "La altura es requerida es requerida"}
#validates :height, :allow_blank => true, :length => {:in => 1..9 }
#  validates :long, :presence => {:message => "El largo es requerido"}
# validates :long, :allow_blank => true, :length => {:in => 1..9 }
#  validates :width, :presence => {:message => "El ancho es requerido"}
#validates :width, :allow_blank => true, :length => {:in => 1..9 }
#  validates :weight, :presence => {:message => "El peso es requerido"}
#validates :weight, :allow_blank => true, :length => {:in => 1..9 }
#  validates :state, :presence => {:message => "El estado es requerido"}
#  validates :group, :presence => {:message => "El grupo es requerido"}
# validates :subGroup, :presence => {:message => "El subgrupo es requerido"}
# validates :tipology, :presence => {:message => "La tipologia es requerido"}
# validates :typeCulturalInstitution, :presence => {:message => "El tipo de institución cultural es requerido"}
# validates :culturalInstitution, :presence => {:message => "La institución cultural es requerido"}
# validates :period, :presence => {:message => "El periodo es requerido"}
#  validates :culture, :presence => {:message => "La cultura es requerido"}
# validates :style, :presence => {:message => "El estilo es requerido"}
#  validates :owner, :presence => {:message => "El propietario es requerido"}
#  validates :author, :presence => {:message => "El autor es requerido"}
#  validates :conservationalState, :presence => {:message => "El estado de conservación es requerido"}
#  validates :integrityState, :presence => {:message => "El estado de integridad es requerido"}
#  validates :shape, :presence => {:message => "La forma es requerido"}
#  validates :technique, :presence => {:message => "La tecnica es requerido"}
#  validates :topic, :presence => {:message => "El tema es requerido"}
#  validates :delete, :presence => {:message => "El borrado es requerido"}


  def self.all_non_deleted
    return find(:all,:conditions=>{:deleted=>[0]},:select=>"cultural_heritage_cultural_heritages.materials,cultural_heritage_cultural_heritages.id,cultural_heritage_cultural_heritages.name,cultural_heritage_cultural_heritages.updated_at,cultural_heritage_cultural_heritages.nationalRegistrationCode,cultural_heritage_cultural_heritages.description,cultural_heritage_cultural_heritages.foundInId,cultural_heritage_cultural_heritages.height,cultural_heritage_cultural_heritages.long,cultural_heritage_cultural_heritages.width,cultural_heritage_cultural_heritages.weight,cultural_heritage_cultural_heritages.state,cultural_heritage_cultural_heritages.group,cultural_heritage_cultural_heritages.subGroup,cultural_heritage_cultural_heritages.tipology,cultural_heritage_cultural_heritages.typeCulturalInstitution,cultural_heritage_cultural_heritages.culturalInstitution,cultural_heritage_cultural_heritages.period,cultural_heritage_cultural_heritages.culture,cultural_heritage_cultural_heritages.style,cultural_heritage_cultural_heritages.owner,cultural_heritage_cultural_heritages.author,cultural_heritage_cultural_heritages.conservationalState,cultural_heritage_cultural_heritages.integrityState,cultural_heritage_cultural_heritages.shape,cultural_heritage_cultural_heritages.technique,cultural_heritage_cultural_heritages.topic")
  end

  def str_state
    if state.blank?
      return ""
    else
      obj_state = CulturalHeritage::State.find(state)
      return obj_state.name
    end
  end

  def str_group
    if group.blank?
      return ""
    else
      obj_group = CulturalHeritage::Group.find(group)
      return obj_group.name
    end
  end

  def str_subGroup
    if subGroup.blank?
      return ""
    else
      obj_subGroup = CulturalHeritage::SubGroup.find(subGroup)
      return obj_subGroup.name
    end
  end

  def str_tipology
    if tipology.blank?
      return ""
    else
      obj_tipology = CulturalHeritage::Tipology.find(tipology)
      return obj_tipology.name
    end
  end

  def str_typeCulturalInstitution
    if typeCulturalInstitution.blank?
      return ""
    else
      obj_typeCulturalInstitution = CulturalHeritage::TypeCulturalInstitution.find(typeCulturalInstitution)
      return obj_typeCulturalInstitution.name
    end
  end

  def str_culturalInstitution
    if culturalInstitution.blank?
      return ""
    else
      obj_culturalInstitution = CulturalHeritage::CulturalInstitution.find(culturalInstitution)
      return obj_culturalInstitution.name
    end
  end

  def str_period
    if period.blank?
      return ""
    else
      obj_period = CulturalHeritage::Period.find(period)
      return obj_period.name
    end
  end

  def str_culture
    if culture.blank?
      return ""
    else
      obj_culture = CulturalHeritage::Culture.find(culture)
      return obj_culture.name
    end
  end

  def str_style
    if style.blank?
      return ""
    else
      obj_style = CulturalHeritage::Style.find(style)
      return obj_style.name
    end
  end

  def str_owner
    if owner.blank?
      return ""
    else
      obj_person = Security::Person.find(owner)
      return obj_person.name
    end
  end

  def str_author
    if author.blank?
      return ""
    else
      obj_author = Security::Person.find(author)
      return obj_author.name
    end
  end

  def str_conservationalState
    if conservationalState.blank?
      return ""
    else
      obj_conservationalState = CulturalHeritage::ConservationalState.find(conservationalState)
      return obj_conservationalState.name
    end
  end

  def str_integrityState
    if integrityState.blank?
      return ""
    else
      obj_integrityState = CulturalHeritage::IntegrityState.find(integrityState)
      return obj_integrityState.name
    end
  end

  def str_shape
    if shape.blank?
      return ""
    else
      obj_shape = CulturalHeritage::Shape.find(shape)
      return obj_shape.name
    end
  end

  def str_technique
    if technique.blank?
      return ""
    else
      obj_technique = CulturalHeritage::Technique.find(technique)
      return obj_technique.name
    end
  end

  def str_topic
    if topic.blank?
      return ""
    else
      obj_topic = CulturalHeritage::Topic.find(topic)
      return obj_topic.name
    end    
  end

  def self.count_state_dependencies(id)
      return find(:all,:conditions=>["(cultural_heritage_cultural_heritage_pre_registers.deleted = ?) and (cultural_heritage_cultural_heritage_pre_registers.state  = ?) ",0,id],:select=>"count(*) as 'cultural_heritage_pre_registers'")[0].cultural_heritage_pre_registers.to_i
  end

  def self.count_group_dependencies(id)
      return find(:all,:conditions=>["(cultural_heritage_cultural_heritage_pre_registers.deleted = ?) and (cultural_heritage_cultural_heritage_pre_registers.group  = ?) ",0,id],:select=>"count(*) as 'cultural_heritage_pre_registers'")[0].cultural_heritage_pre_registers.to_i
  end

  def self.count_subgroup_dependencies(id)
      return find(:all,:conditions=>["(cultural_heritage_cultural_heritage_pre_registers.deleted = ?) and (cultural_heritage_cultural_heritage_pre_registers.subGroup  = ?) ",0,id],:select=>"count(*) as 'cultural_heritage_pre_registers'")[0].cultural_heritage_pre_registers.to_i
  end

  def self.count_tipology_dependencies(id)
      return find(:all,:conditions=>["(cultural_heritage_cultural_heritage_pre_registers.tipology  = ?) and (cultural_heritage_cultural_heritage_pre_registers.deleted = ?) ",id,0],:select=>"count(*) as 'cultural_heritage_pre_registers'")[0].cultural_heritage_pre_registers.to_i
  end

  def self.count_typeCulturalInstitution_dependencies(id)
      return find(:all,:conditions=>["(cultural_heritage_cultural_heritage_pre_registers.typeCulturalInstitution  = ?) and (cultural_heritage_cultural_heritage_pre_registers.deleted = ?) ",id,0],:select=>"count(*) as 'cultural_heritage_pre_registers'")[0].cultural_heritage_pre_registers.to_i
  end

  def self.count_culturalInstitution_dependencies(id)
      return find(:all,:conditions=>["(cultural_heritage_cultural_heritage_pre_registers.culturalInstitution  = ? and cultural_heritage_cultural_heritage_pre_registers.typeCulturalInstitution  = ?) and (cultural_heritage_cultural_heritage_pre_registers.deleted = ?) ",id,1,0],:select=>"count(*) as 'cultural_heritage_pre_registers'")[0].cultural_heritage_pre_registers.to_i
  end

  def self.count_period_dependencies(id)
      return find(:all,:conditions=>["(cultural_heritage_cultural_heritage_pre_registers.period  = ?) and (cultural_heritage_cultural_heritage_pre_registers.deleted = ?) ",id,0],:select=>"count(*) as 'cultural_heritage_pre_registers'")[0].cultural_heritage_pre_registers.to_i
  end

  def self.count_culture_dependencies(id)
      return find(:all,:conditions=>["(cultural_heritage_cultural_heritage_pre_registers.culture  = ?) and (cultural_heritage_cultural_heritage_pre_registers.deleted = ?) ",id,0],:select=>"count(*) as 'cultural_heritage_pre_registers'")[0].cultural_heritage_pre_registers.to_i
  end

  def self.count_style_dependencies(id)
      return find(:all,:conditions=>["(cultural_heritage_cultural_heritage_pre_registers.style  = ?) and (cultural_heritage_cultural_heritage_pre_registers.deleted = ?) ",id,0],:select=>"count(*) as 'cultural_heritage_pre_registers'")[0].cultural_heritage_pre_registers.to_i
  end

  def self.count_person_dependencies(id)
      return find(:all,:conditions=>["(cultural_heritage_cultural_heritage_pre_registers.owner  = ? or cultural_heritage_cultural_heritage_pre_registers.author  = ?) and (cultural_heritage_cultural_heritage_pre_registers.deleted = ?) ",id,id,0],:select=>"count(*) as 'cultural_heritage_pre_registers'")[0].cultural_heritage_pre_registers.to_i
  end

  def self.count_conservationalState_dependencies(id)
      return find(:all,:conditions=>["(cultural_heritage_cultural_heritage_pre_registers.conservationalState  = ?) and (cultural_heritage_cultural_heritage_pre_registers.deleted = ?) ",id,0],:select=>"count(*) as 'cultural_heritage_pre_registers'")[0].cultural_heritage_pre_registers.to_i
  end

  def self.count_integrityState_dependencies(id)
      return find(:all,:conditions=>["(cultural_heritage_cultural_heritage_pre_registers.integrityState  = ?) and (cultural_heritage_cultural_heritage_pre_registers.deleted = ?) ",id,0],:select=>"count(*) as 'cultural_heritage_pre_registers'")[0].cultural_heritage_pre_registers.to_i
  end

  def self.count_shape_dependencies(id)
      return find(:all,:conditions=>["(cultural_heritage_cultural_heritage_pre_registers.shape  = ?) and (cultural_heritage_cultural_heritage_pre_registers.deleted = ?) ",id,0],:select=>"count(*) as 'cultural_heritage_pre_registers'")[0].cultural_heritage_pre_registers.to_i
  end

  def self.count_technique_dependencies(id)
      return find(:all,:conditions=>["(cultural_heritage_cultural_heritage_pre_registers.technique  = ?) and (cultural_heritage_cultural_heritage_pre_registers.deleted = ?) ",id,0],:select=>"count(*) as 'cultural_heritage_pre_registers'")[0].cultural_heritage_pre_registers.to_i
  end

  def self.count_topic_dependencies(id)
      return find(:all,:conditions=>["(cultural_heritage_cultural_heritage_pre_registers.topic  = ?) and (cultural_heritage_cultural_heritage_pre_registers.deleted = ?) ",id,0],:select=>"count(*) as 'cultural_heritage_pre_registers'")[0].cultural_heritage_pre_registers.to_i
  end

  def self.count_cultural_heritage_dependencies(id)
      return find(:all,:conditions=>["(cultural_heritage_cultural_heritage_pre_registers.foundInId = ? or cultural_heritage_cultural_heritage_pre_registers.partOf = ? or cultural_heritage_cultural_heritage_pre_registers.collection = ? or (cultural_heritage_cultural_heritage_pre_registers.culturalInstitution  = 1 and cultural_heritage_cultural_heritage_pre_registers.typeCulturalInstitution  = ?)) and (cultural_heritage_cultural_heritage_pre_registers.deleted = ? )",id,id,id,id,0],:select=>"count(*) as 'cultural_heritage_pre_registers'")[0].cultural_heritage_pre_registers.to_i
  end

  def self.count_pre_register_request_dependencies(id)
      return find(:all,:conditions=>["(cultural_heritage_cultural_heritage_pre_registers.cultural_heritage_pre_register_request_id  = ?) and (cultural_heritage_cultural_heritage_pre_registers.deleted = ?) ",id,0],:select=>"count(*) as 'cultural_heritage_pre_registers'")[0].cultural_heritage_pre_registers.to_i
  end
end
